<script lang="ts" setup>

import {ElMessageBox} from "element-plus";
import {useUserStore} from "@/stores/user";
import {useRouter} from "vue-router";
import {useMenuStore} from "@/stores/menu";
import type {Profile} from "@/types/user";
const store = useUserStore()
const router = useRouter()
const handleCommand = (command: string)=>{
  if (command === 'logout'){
    logout()
  }
}
const logout = async ()=>{
  const res = await ElMessageBox.confirm(
      '您确定要退出登录吗?',
      '温馨提示',
      {
        confirmButtonText: '残忍离开',
        cancelButtonText: '我再想想',
        type: 'warning',
      }
  )
  if (res === 'confirm'){
  //   清楚本地数据
    store.setToken('')
    // 清除用户权限对象
    store.profile = {} as Profile
    // 清除路由和左侧菜单
    const menuStore= useMenuStore()
    menuStore.clearMenuList()
  //   跳转到登录页
    router.replace('/login')
  }
}
</script>

<template>
  <div class="nav-bar">
    <!-- breadcrumb -->
    <div></div>
    <!-- dropdown -->
    <el-dropdown @command="handleCommand">
      <div class="user-wrapper">{{ store.profile.name }}</div>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item>首页</el-dropdown-item>
          <el-dropdown-item>项目地址</el-dropdown-item>
          <el-dropdown-item command="logout">退出登录</el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<style lang="scss" scoped>
.nav-bar {
  height: 60px;
  overflow: hidden;
  position: relative;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;

  .user-wrapper {
    outline: none;
    cursor: default;
    color: var(--el-color-black);
  }
}
</style>
